﻿using System;
using System.Collections.Generic;
using CosyTrade.Core;
using CosyTrade.Data;
using CosyTrade.Futures;
using NHibernate.Expression;
using CosyTrade.Store;

namespace CosyTrade
{
    [Addin("FuturesOrderManager")]
    public class FuturesOrderManager : AddinBase, IFuturesManager
    {
        #region IFuturesManager 成员

        public void CreateFuturesOrder(FuturesOrder order)
        {
            SaveChilds(order,true);
            OrmDataAccess.Create(order);
        }

        public void UpdateFuturesOrder(FuturesOrder order)
        {
            OrmDataAccess.Save(order);
        }

        public void UpdateOrderAndItChilds(FuturesOrder order)
        {
            SaveChilds(order, false);
            OrmDataAccess.Save(order);
        }

        public void UpdateWares(Guid orderId, OrderFormWares wares)
        {
            
        }

        public void UpdatePayInfo(Guid orderId, PayInfo info)
        {
            
        }

        public void UpdateDelayInfo(Guid order, DelayPay delayPay)
        {
            
        }

        public void DeleteFuturesOrder(Guid orderId)
        {
            FuturesOrder order = GetFuturesOrder(orderId);
            if (order != null)
            {
                OrmDataAccess.Delete(order);
            }
        }

        public void DeletePayinfo(Guid infoId)
        {
            PayInfo info = GetPayInfo(infoId);
            if(info!=null)
                OrmDataAccess.Delete(info);
        }

        public void DeleteOrderWares(Guid waresid)
        {
            OrderFormWares wares = GetOrderFormWares(waresid);
            if(wares!=null)
                OrmDataAccess.Delete(wares);
        }

        public void DeleteDelayinfo(Guid delayid)
        {
            DelayPay pay = GetDelayPayInfo(delayid);
            if(pay!=null)
                OrmDataAccess.Delete(pay);
        }

        public IList<FuturesOrder> GetFuturesOrders(ICriterion criterion)
        {
            return OrmDataAccess.FindAll<FuturesOrder>(criterion);
        }

        public IList<FuturesOrder> GetFuturesOrders(int pageIndex, int pageSize, ICriterion criterion, Order[] orderby)
        {
            return OrmDataAccess.SlicedFindAll<FuturesOrder>(pageSize*pageIndex,pageSize,orderby,criterion);
        }

        public IList<OrderFormWares> GetFormWares(Guid orderId)
        {
            FuturesOrder order = GetFuturesOrder(orderId);
            if (order != null) return order.OrderWares;
            return null;
        }

        public IList<PayInfo> GetPayInfos(Guid orderId)
        {
            FuturesOrder order = GetFuturesOrder(orderId);
            if (order != null) return order.OrderPayInfos;
            return null;
        }

        public IList<DelayPay> GetDelays(Guid orderId)
        {
            FuturesOrder order = GetFuturesOrder(orderId);
            if (order != null) return order.DelayPayInfo;
            return null;
        }

        void SaveChilds(FuturesOrder order,bool isnew)
        {
            if(isnew)
            {
                foreach (OrderFormWares ware in order.OrderWares)
                    OrmDataAccess.Create(ware);
                foreach(PayInfo info in order.OrderPayInfos)
                    OrmDataAccess.Create(info);
                foreach(DelayPay delayPay in order.DelayPayInfo)
                    OrmDataAccess.Create(delayPay);
            }
            else
            {
                foreach (OrderFormWares ware in order.OrderWares)
                {
                    OrderFormWares ofw = GetOrderFormWares(ware.PrimaryKey);
                    if(ofw!=null) OrmDataAccess.Save(ofw);
                    else OrmDataAccess.Create(ware);
                }
                foreach (PayInfo info in order.OrderPayInfos)
                {
                    PayInfo inf = GetPayInfo(info.PrimaryKey);
                    if(inf!=null) OrmDataAccess.Save(inf);
                    else OrmDataAccess.Create(info);
                }
                foreach (DelayPay delayPay in order.DelayPayInfo)
                {
                    DelayPay pay = GetDelayPayInfo(delayPay.PrimaryKey);
                    if(pay!=null) OrmDataAccess.Save(pay);
                    else OrmDataAccess.Create(delayPay);
                }
            }
        }
        
        public FuturesOrder GetFuturesOrder(Guid primarykey)
        {
            try
            {
                return OrmDataAccess.FindByPrimaryKey<FuturesOrder>(primarykey);
            }
            catch(Exception ex)
            {
                return null;
            }
        }

        public OrderFormWares GetOrderFormWares(Guid primarykey)
        {
            try
            {
                return OrmDataAccess.FindByPrimaryKey<OrderFormWares>(primarykey);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public PayInfo GetPayInfo(Guid primarykey)
        {
            try
            {
                return OrmDataAccess.FindByPrimaryKey<PayInfo>(primarykey);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public DelayPay GetDelayPayInfo(Guid primarykey)
        {
            try
            {
                return OrmDataAccess.FindByPrimaryKey<DelayPay>(primarykey);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public IList<Product> GetProductList(ICriterion criterion)
        {
            return OrmDataAccess.FindAll<Product>(criterion);
        }

        #endregion
    }
}
